using System;
using System.Collections.Generic;

using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;

using FunctionInterface;
using System.Threading;

namespace PhoneConsole
{
	[Activity (Label = "PhoneConsole", MainLauncher = true)]
	public class Activity1 : Activity
	{
		private CommandDispatcher dispatcher = null;
		private LinearLayout ll = null;
		private EditText etCommand = null;
		private LinearLayout llCommands = null;
		private ScrollView sv = null;
		//private Thread thread = null;
		
		protected override void OnCreate (Bundle bundle)
		{
			base.OnCreate (bundle);
			// Set our view from the "main" layout resource
			SetContentView (Resource.Layout.Main);
			
			InitActivity ();
		}

		protected void InitActivity ()
		{
			//PendingIntent pi = PendingIntent.GetBroadcast(null, 0, new Intent(), PendingIntentFlags.NoCreate);
			llCommands = FindViewById<LinearLayout>(Resource.Id.llCommands);
			sv = FindViewById<ScrollView>(Resource.Id.sv);
			
			TextView tv = FindViewById<TextView>(Resource.Id.textView1);
			tv.Text = @"-----wangyang:wyangvip@163.com-----";
			ll = FindViewById<LinearLayout> (Resource.Id.llText);
			etCommand = FindViewById<EditText> (Resource.Id.etCommand);
			etCommand.KeyPress += (object sender, View.KeyEventArgs e) => 
			{
				if(e.KeyCode == Keycode.Back)
				{
					this.Finish ();
					ActivityManager am = (ActivityManager)this.GetSystemService(Android.Content.Context.ActivityService);
					am.RestartPackage(this.PackageName);
					return;
				}
				if (e.E.Action == KeyEventActions.Down && e.KeyCode == Keycode.Enter) 
				{
					Process();
					//Toast.MakeText (this, etCommand.Text, ToastLength.Short).Show ();
				}
				e.Handled = false;
			};
			// Get our button from the layout resource,
			// and attach an event to it
			Button btnOK = FindViewById<Button> (Resource.Id.btnOK);
			btnOK.Click += delegate(object sender, EventArgs e) 
			{
				//StartNewActivity();
				Process();
			};
						
			BuildCommandDipather();
			
//			thread = new Thread(new ThreadStart(delegate
//			{
//				OutputMessage("nice...");
//			}));
//			thread.Start ();
		}
		
		/// <summary>
		/// Raises the key down event.
		/// </summary>
		/// <param name='keyCode'>
		/// If set to <c>true</c> key code.
		/// </param>
		/// <param name='e'>
		/// If set to <c>true</c> e.
		/// </param>
		public override bool OnKeyDown (Keycode keyCode, KeyEvent e)
		{
			if(keyCode == Keycode.Back)
			{
				//return false;
				this.Finish ();
				return true;
			}
			return base.OnKeyDown (keyCode, e);
		}
		
		/// <summary>
		/// Outputs the message.
		/// </summary>
		/// <param name='str'>
		/// String.
		/// </param>
	    protected void OutputMessage(string str)
		{
			// 处理命令
			TextView tv1 = new TextView(this.BaseContext);
			tv1.Text = str;
			ll.AddView(tv1);
				
			sv.Post(delegate 
			{
				sv.FullScroll(FocusSearchDirection.Down);
			});
		}

		/// <summary>
		/// Returns the main process.
		/// </summary>
		protected void BuildCommandDipather()
		{
			dispatcher = new CommandDispatcher(new FunctionInterface.Context()
			{
				MainActivity = this
			});
			dispatcher.OutputMessage += (sender, e) => 
			{
				OutputMessage(e.ToString ());
			};
			dispatcher.ProcessChanged += (sender, e) => 
			{
				// 内部命令列表
				foreach(var obj in e.CurProcessCommand.GetCommnads())
				{
					OutputMessage(obj.ToString ());
				}
			};
			IEnumerator<string> lst = dispatcher.GetProcessCommands();
			while(lst.MoveNext())
			{
				OutputMessage(lst.Current.ToString ());
			}
		}
		
		/// <summary>
		/// Process this instance.
		/// </summary>
		protected void Process()
		{
			if(etCommand.Text.Trim ().ToLower() == "q")
			{
				dispatcher.CurProcess = null;
				IEnumerator<string> lst = dispatcher.GetProcessCommands();
				while(lst.MoveNext())
				{
					OutputMessage(lst.Current.ToString ());
				}
				return;
			}
			dispatcher.Process(etCommand.Text);
			
			etCommand.Text = string.Empty;
		}	
		
		#region service
		//StartService(new Intent(this, typeof(InterService)));
		//StopService(new Intent(this, typeof(InterService)));
		#endregion
		
		#region Start new activity
		protected void StartNewActivity()
		{
			var activity2 = new Intent (this, typeof(ShowActivity));
  	        activity2.PutExtra ("MyData", "Data from Activity1");
	        StartActivity (activity2);  
		}
		#endregion
	}
}



